package com.dp.mallchat.common.user.dao;

import com.dp.mallchat.common.common.domain.enums.YesOrNoEnum;
import com.dp.mallchat.common.user.domain.user.entity.UserBackpack;
import com.dp.mallchat.common.user.domain.user.enums.ItemTypeEnum;
import com.dp.mallchat.common.user.mapper.UserBackpackMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户背包表 服务实现类
 * </p>
 *
 * @author <a href="https://gitee.com/baiDP">adu</a>
 * @since 2024-06-02
 */
@Service
public class UserBackpackDao extends ServiceImpl<UserBackpackMapper, UserBackpack> {

    @Autowired
    private UserBackpackMapper userBackpackMapper;

    public Integer getCountByUidAndItemId(Long uid, Long id) {
        return lambdaQuery().eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, id)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .count();
    }

    public UserBackpack getFistValidItem(Long uid, Long itemId) {
        return lambdaQuery().eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .orderByAsc(UserBackpack::getCreateTime)
                .last("limit 1")
                .one();
    }


    public boolean updateCountByUidAndItemId(Long id) {
        return lambdaUpdate().set(UserBackpack::getStatus, YesOrNoEnum.YES.getStatus())
                .eq(UserBackpack::getId, id)
                .update();
    }

    public List<UserBackpack> listBadgeByUid(Long uid, List<Long> itemId) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .in(UserBackpack::getItemId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .list();
    }

    public UserBackpack getByIdempontence(String idempotence) {
        return lambdaQuery()
                .eq(UserBackpack::getIdempotent, idempotence)
                .one();
    }

    public List<UserBackpack> getByItemIds(List<Long> uids, List<Long> itemIds) {
        return lambdaQuery().in(UserBackpack::getUid, uids)
                .in(UserBackpack::getItemId, itemIds)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .list();
    }
}
